﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using WcfFlashRemoting.ServiceModel.Channels;

namespace WcfFlashRemoting.ServiceModel.Configuration
{
    public sealed class AmfMessageEncodingBindingElement : MessageEncodingBindingElement
    {
        public override MessageEncoderFactory CreateMessageEncoderFactory()
        {
            return new AmfMessageEncoderFactory();
        }

        public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
        {
            context.BindingParameters.Add(this);
            return base.BuildChannelFactory<TChannel>(context);
        }

        public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
        {
            context.BindingParameters.Add(this);
            return base.BuildChannelListener<TChannel>(context);
        }

        public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
        {
            context.BindingParameters.Add(this);
            return base.CanBuildChannelFactory<TChannel>(context);
        }

        public override bool CanBuildChannelListener<TChannel>(BindingContext context)
        {
            context.BindingParameters.Add(this);
            return base.CanBuildChannelListener<TChannel>(context);
        }

        public override MessageVersion MessageVersion
        {
            get
            {
                return MessageVersion.None;
            }
            set
            {
                if (value != MessageVersion.None)
                {
                    throw new ArgumentException();
                }
            }
        }

        public override BindingElement Clone()
        {
            return new AmfMessageEncodingBindingElement()
            {
                MessageVersion = MessageVersion.None
            };
        }
    }
}
